package com.rxst.msg.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;

/**
 * AESUtils
 *
 * @author
 * @date
 */
@Slf4j
public class AESUtils {

    /**
     * 加密算法
     */
    private static final String KEY_ALGORITHMS = "AES";
    /**
     * 字符编码
     */
    private static final String CHAR_SETS = "UTF-8";

    public static final String KEY = "asdqwersjlk12hsa";
    /**
     * AES的密钥长度
     */
    private static final Integer SECRET_KEY_LENGTHS = 128;
    /**
     * 加解密算法/工作模式/填充方式
     */
    private static final String DEFAULT_CIPHER_ALGORITHMS = "AES/ECB/PKCS5Padding";

    /**
     * @param password
     * @return
     * @throws NoSuchAlgorithmException
     */
    private static SecretKeySpec getSecretKey(final String password) throws NoSuchAlgorithmException {
        //生成指定算法密钥的生成器
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHMS);
        keyGenerator.init(SECRET_KEY_LENGTHS, new SecureRandom(password.getBytes()));
        //生成密钥
        SecretKey secretKey = keyGenerator.generateKey();
        //转换成AES的密钥
        return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHMS);
    }

    public static String aesEncrypt(String sSrc) {
        return aesEncrypt(sSrc, KEY);
    }

    /**
     * 加密
     *
     * @param sSrc       待加密字符串
     * @param privateKey 私钥
     * @return
     */
    public static String aesEncrypt(String sSrc, String privateKey) {
        if (sSrc == null || "".equals(sSrc)) {
            return null;
        }
        byte[] encrypted = new byte[0];
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHMS);
            byte[] raw = privateKey.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHMS);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            encrypted = cipher.doFinal(sSrc.getBytes(CHAR_SETS));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Base64.encodeBase64String(encrypted);
    }

    public static String aesDecrypt(String sSrc) {
        return aesDecrypt(sSrc, KEY);
    }

    /**
     * 解密
     *
     * @param sSrc       待解密字符串
     * @param privateKey 私钥
     * @return
     */
    public static String aesDecrypt(String sSrc, String privateKey) {
        if (sSrc == null || "".equals(sSrc)) {
            return null;
        }
        try {
            byte[] raw = privateKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHMS);
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHMS);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = Base64.decodeBase64(sSrc.getBytes(CHAR_SETS));
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, CHAR_SETS);
            return originalString;
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(ex.getMessage());
            return null;
        }
    }

    public static void main(String[] args) {
        String content = "18210132343";
        String key = "asdqwersjlk12hsa";
        System.out.println("原文=" + content);
        String s1 = aesEncrypt(content, key);
        System.out.println("加密结果=" + s1);
        System.out.println("解密结果=" + aesDecrypt(s1, key));
//        DateUtil.offsetDay(DateUtil.date(),-1);
//        String dateStr = "2021-08-23 17:50:00";
//        Date a = DateUtil.parse(dateStr);
//        long betweenDay = DateUtil.betweenDay(DateUtil.date(), a,false);
//        System.out.println(betweenDay);

//        String fileName = "李四-202108261530.mp4";
//        String prefix = fileName.substring(fileName.lastIndexOf("."));
//        String name = fileName.substring(0,fileName.lastIndexOf("."));
//        System.out.println(prefix);
//        System.out.println(name);
//
//        String[] split = fileName.split("-");
//        if(split != null && split.length > 1){
//            String str = split[1];
//            String time = str.substring(0, 4) + "-" + str.substring(4, 6) + "-" + str.substring(6, 8) + " " + str.substring(8, 10) + ":" +  str.substring(10, 12);
//            System.out.println(time);
//            DateTime parse = DateUtil.parse(time);
//            System.out.println(parse.toString("yyyy-MM-dd HH:mm"));
//        }

//        System.out.println(DateUtil.format(DateUtil.date(),"yyyy/MM"));
//        System.out.println(DateUtil.format(DateUtil.date(), "yyyy" + File.separator + "MM"));
//        System.out.println(File.separator);
//
//        String path = "D://myworkspace//rxst-msg//test//";
//        String format = DateUtil.format(DateUtil.date(), "yyyy" + File.separator + "MM");
//        File parent = new File(path + format);
//        if (!parent.exists()) {
//            parent.mkdirs();
//        }

    }

}
